package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.controller.dto.FeedbackRequestDTO;
import com.yqg.education.entity.StuFeedback;
import com.yqg.education.entity.TeachEvaluate;
import com.yqg.education.mapper.StuFeedbackMapper;
import com.yqg.education.service.IStuFeedbackService;
import com.yqg.education.service.ITeachEvaluateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * StuFeedbackController 前端控制器
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/stu-feedback")
public class StuFeedbackController {

    @Resource
    private ITeachEvaluateService teachEvaluateService;
    @Resource
    private IStuFeedbackService stuFeedbackService;

    @Resource
    private StuFeedbackMapper stuFeedbackMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody StuFeedback stuFeedback) {
        stuFeedback.setCreateTime(LocalDateTime.now());
        return Result.success(stuFeedbackService.saveOrUpdate(stuFeedback));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        StuFeedback stuFeedback =stuFeedbackService.getById(id);
        stuFeedback.setIsDelete(1);
        return Result.success(stuFeedbackService.saveOrUpdate(stuFeedback));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<StuFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<StuFeedback> list=  stuFeedbackService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(stuFeedbackService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           String name,
                           @RequestParam(required = false) Integer courseId) {
        Integer pageStartNumber = pageNum * pageSize - pageSize;
        Map<String,Object> map = new HashMap<>();
        List<StuFeedback> courseEvaluationList = stuFeedbackMapper.findPage(pageStartNumber, pageSize, name, courseId);
        Integer count = stuFeedbackMapper.findCount(name, courseId);
        map.put("records",courseEvaluationList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端获取课程评价
     */
    @GetMapping("/stuFindCourseEvaluation")
    public Result stuFindCourseEvaluation(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         @RequestParam Integer courseId){
        Integer pageStartNumber = pageNum * pageSize - pageSize;
        Map<String,Object> map = new HashMap<>();
        List<StuFeedback> courseEvaluationList = stuFeedbackMapper.findCourseEvaluation(pageStartNumber, pageSize, courseId);
        Integer count = stuFeedbackMapper.findCourseEvaluationCount(courseId);
        map.put("records",courseEvaluationList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端反馈接口
     */
    @PostMapping("stuFeedbackSave")
    public Result stuFeedbackSave(@RequestBody FeedbackRequestDTO feedback) {
        boolean result = false;
        if (feedback.getType().equals("1")) {
            // 课程反馈
            StuFeedback stuFeedback = new StuFeedback();
            stuFeedback.setStuId(feedback.getStuId());
            stuFeedback.setCourseId(feedback.getCourseId());
            stuFeedback.setPeriodFeedback(feedback.getPeriodFeedback());
            stuFeedback.setCreateTime(LocalDateTime.now());
            result = stuFeedbackService.save(stuFeedback);
        }else if (feedback.getType().equals("2")) {
            // 教师反馈
            TeachEvaluate teachEvaluate = new TeachEvaluate();
            teachEvaluate.setTeachId(feedback.getTeachId());
            teachEvaluate.setStuId(feedback.getStuId());
            teachEvaluate.setEvaluate(feedback.getPeriodFeedback());
            teachEvaluate.setCreateTime(LocalDateTime.now());
            result = teachEvaluateService.save(teachEvaluate);
        }
        if (result){
            return Result.success();
        }else{
            return Result.error("反馈失败");
        }
    }
}

